package com.rykj.energy.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.File;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

@Slf4j
public class HttpFileRequest {
    /**
     * 从网络Url中下载文件
     * @param urlStr
     * @param fileName
     * @param savePath
     * @throws IOException
     */
    public static void  downLoadFromUrl(String urlStr,String fileName,String savePath,String token){
        try {
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            //设置超时间为3秒
            conn.setConnectTimeout(3*1000);
            //防止屏蔽程序抓取而返回403错误
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            conn.setRequestProperty("lfwywxqyh_token",token);

            //得到输入流
            InputStream inputStream = conn.getInputStream();
            //获取自己数组
           // byte[] getData = readInputStream(inputStream);

            //文件保存位置
            File saveDir = new File(savePath);
            if(!saveDir.exists()){
                saveDir.mkdirs();
            }
            File file = new File(saveDir+File.separator+fileName);
            FileOutputStream fos = new FileOutputStream(file);
            byte[] buffer = new byte[1024*10];
            int bytesum = 0;
            int byteread = 0;
            while ((byteread = inputStream.read(buffer)) != -1) {
                bytesum += byteread;
//                log.info("dddd"+bytesum);
                fos.write(buffer, 0, byteread);
            }
          //  fos.write(getData);
            if(fos!=null){
                fos.close();
            }
            if(inputStream!=null){
                inputStream.close();
            }
            if(bytesum == 0){
                log.error("下载消息附件失败,info=[" + JsonUtil.beanToString(urlStr) + "]");
            }

            System.out.println("info:"+url+" download success=====tagFile==="+saveDir+File.separator+fileName);
        }catch (Exception e){
            e.printStackTrace();
            log.error("下载消息附件失败,info=[" + JsonUtil.beanToString(urlStr) + "]");
        }


    }



    /**
     * 从输入流中获取字节数组
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static  byte[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024*10];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }



    public static void main(String[] args) throws IOException {
        // write your code here

        ;
        String url="http://192.168.0.100:7771/temp/lssb/rtsp4.53.68.7_4552_0/2264d04d-0e4f-4861-984c-f3658bd7cf8d.jpg";

        String token="v32Eo2Tw+qWI/eiKW3D8ye7l19mf1NngRLushO6CumLMHIO1aryun0/Y3N3YQCv/TqzaO/TFHw4=";
        // String token="SiGBCH6QblUHs7NiouV09rL6uAA3Sv0cGicaSxJiC/78DoWIMzVbW6VCwwkymYsZaxndDkYqkm4=";
        String[] tmp=url.split("/");
        HttpFileRequest.downLoadFromUrl(url,tmp[tmp.length-1],"D:\\",token);
        System.out.println("下载完成");

    }



}